﻿using System;
using Framework.MVC;
using Log.Model;
using Extention.Model;
using Extention.Model.Behavior;
using Framework.MVVM.Model;
using Common.IService;
using Log.IService;
using Common.Service;
using Log.Service;
using Identity.IService;
using Identity.Service;



namespace FrontCenter.Models
{
    [ServiceAttribute(typeof(Extention.IService.IOrderService), typeof(Extention.Service.OrderService), "Instance")]
    [ServiceAttribute(typeof(IIdentityService), typeof(IdentityService), "Instance")]
    [ServiceAttribute(typeof(ILogService), typeof(LogService), "Instance")]
    [ServiceAttribute(typeof(ICodeService), typeof(CodeService), "Instance")]
    public class QueryPageOrderModel : AsyncModelBase, IWriteLog, Identity.Model.IValidateIdentity, IQueryPageOrder
    {
        #region Field

        private const char m_split = ';';

        private const string m_empty_identity = "qryorder0";
        private const string m_error_identity = "qryorder1";
        private const string m_complete_identity = "qryorder2";
        private const string m_query_error = "qryorder3";
        private const string m_query_complete = "qryorder4";

        private const string m_resource_user = "u000";

        private const string m_count = "count";
        private const string m_page = "page";
        private const string m_size = "size";
        private const string m_list = "list";

        private const string m_error = "error";

        #endregion Field

        #region AsyncModelBase

        protected override void OnInit(Type type)
        {
            OnBeginRequest();
            Identity.Model.IdentityBehavior.ValidateIdentity(this, m_resource_user, m_empty_identity, m_error_identity, m_complete_identity);
        }

        #endregion AsyncModelBase

        #region Write Log

        public void OnWriteLogComplete(string eventCode)
        {
            OnEndRequest();
        }

        public void OnWriteLogError(string eventCode)
        {
            OnEndRequest();
        }

        #endregion Write Log

        #region IValidateIdentity

        public string IdentityCode { get; set; }
        public string Source { get; set; }
        public bool IsOnce { get; set; }

        public void OnValidateIdentityComplete(string key, Identity.IService.Entry.IdentityEntry entry)
        {
            OnBeginRequest();
            this.Log(key, entry);

            OrderUser = entry.value;
            this.QueryPageOrder(m_query_error,m_query_complete);
        }

        public void OnValidateIdentityError(string key)
        {
            OnComplete(m_error, "identity");
            this.Log(key, new Identity.IService.Entry.IdentityEntry { code = IdentityCode, source = Source, resource = m_resource_user });
        }

        #endregion IValidateIdentity

        #region IQueryPageOrder

        public string OrderBegin { get; set; }

        public string OrderEnd { get; set; }

        public int OrderPage { get; set; }

        public int OrderSize { get; set; }

        public string OrderSource { get; set; }

        public string OrderUser { get; set; }

        public void OnQueryPageOrderComplete(string key, Common.IService.Entry.QueryPageEntry<Extention.IService.Entry.OrderEntry> entry)
        {
            OnComplete(m_count, entry.Count);
            OnComplete(m_page, OrderPage);
            OnComplete(m_size, OrderSize);
            OnComplete(m_list, entry.Data);

            this.Log(key, new Extention.IService.Entry.OrderQueryPageParams { user = OrderUser, resource = OrderSource, begin = OrderBegin, end = OrderEnd, page = OrderPage, size = OrderSize });
        }

        public void OnQueryPageOrderError(string key)
        {
            this.Log(key, new Extention.IService.Entry.OrderQueryPageParams { user = OrderUser, resource = OrderSource, begin = OrderBegin, end = OrderEnd, page = OrderPage, size = OrderSize });
        }

        #endregion IQueryPageOrder
    }
}